package com.hph.springcloudstreamkafkademo.stream.producer;

import com.hph.springcloudstreamkafkademo.stream.source.MyMessageSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.messaging.Source;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

/**
 * 消息生产bean（spring-cloud-stream-kafka方式）
 * @author HPH
 * @date 2019/7/27
 */

@Component
@EnableBinding({Source.class,MyMessageSource.class})
public class MessageProducerBean {

    private final MessageChannel messageChannel;

    private final MessageChannel myMessageChannel;

    public MessageProducerBean(@Qualifier(Source.OUTPUT) MessageChannel messageChannel,
                               @Qualifier(MyMessageSource.HPH)MessageChannel myMessageChannel) {
        this.messageChannel = messageChannel;
        this.myMessageChannel = myMessageChannel;
    }

    /**
     * 发送消息,Payload:消息体
     * @param message 消息内容
     */
    public void send(String message){

        messageChannel.send(MessageBuilder.withPayload(message).build());
    }

    /**
     * 使用自定义的消息source测试
     * 发送消息,Payload:消息体
     * @param message 消息内容
     */
    public void mySend(String message){

        myMessageChannel.send(MessageBuilder.withPayload(message).build());
    }
}
